//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// (C) Copyright 2006 Marvell International Ltd.
// All Rights Reserved
//
/******************************************************************************
**
**  COPYRIGHT (C) 2001, 2002 Intel Corporation.
**
**  This software as well as the software described in it is furnished under
**  license and may only be used or copied in accordance with the terms of the
**  license. The information in this file is furnished for informational use
**  only, is subject to change without notice, and should not be construed as
**  a commitment by Intel Corporation. Intel Corporation assumes no
**  responsibility or liability for any errors or inaccuracies that may appear
**  in this document or any software that may be provided in association with
**  this document. 
**  Except as permitted by such license, no part of this document may be 
**  reproduced, stored in a retrieval system, or transmitted in any form or by
**  any means without the express written consent of Intel Corporation. 
**
**  FILENAME:       dmawince.h
**
**  PURPOSE: Header file for dmawince.c
**                  
******************************************************************************/


#ifndef __DMAWINCE_H__
#define __DMAWINCE_H__
//
// DMA controller mutex to guard against concurrency
//
#define DMAC_MUTEX_NAME			TEXT("DMAC")
#define DMAC_MUTEX_TIMEOUT		1000	//uSeconds


BOOL __stdcall DmaDllEntry 
(
    HANDLE  hinstDLL,
    DWORD   Op,
    LPVOID  lpvReserved
);


// Add -- Start -- pkdebug.  May need to move these items

/**
 * DMAC Channel Allocation Priority  // pkdebug. May need to move this typedef.
 **/
typedef enum
{
   XLLP_DMAC_CHANNEL_PRIORITY_HIGH = 0,
   XLLP_DMAC_CHANNEL_PRIORITY_MEDIUM,
   XLLP_DMAC_CHANNEL_PRIORITY_LOW,
   XLLP_DMAC_CHANNEL_PRIORITY_LOWEST
}XLLP_DMAC_CHANNEL_PRIORITY_T;


/**
 * Represents a contiguous physical block of memory
 **/
typedef struct _PhysBlock
{
    XLLP_UINT32_T   physicalAds;	// Array of physical addresses of each contigious physical block
    XLLP_UINT32_T   blockLength;	// Length of each contigious block

} PhysBlock, *PPhysBlock;

/**
 * A Memory Descriptor List (MDL) structure
 * This stores information about physical mappings of a buffer
 **/
typedef struct _BufferMdl
{
    XLLP_UINT32_T   numEntries;		// Number of entries of physical blocks representing this buffer
    XLLP_UINT8_T*   bufferPtr;		// Virtual Buffer ptr
    PhysBlock       *physBlock;		// Array of Physical blocks for buffer

} BufferMdl, *PBufferMdl;

/**
 * This allows us to allocate descriptors and properly align them (on 16 byte boundaries) as well
 **/
typedef struct _dmaDescInfo
{
    XLLP_UINT8_T*               memPtr;         // Pointer to this block of memory
    XLLP_UINT32_T               numEntries;     // Number of descriptors 
    P_XLLP_DMAC_DESCRIPTOR_T    pDmaDescList;	// Array of properly aligned DMA descriptors

} DmaDescInfo, *PDmaDescInfo;

/**
 * DMAC Descriptor Branching Mode  // ?? No longer in the MH DevMan.
 **/
typedef enum
{
    XLLP_DMAC_DISABLE_DESC_BRANCH = 0,
    XLLP_DMAC_ENABLE_DESC_BRANCH
}XLLP_DMAC_DESC_BRANCH_T;



typedef struct _dmaGlobals
{

    //
    // Event used to signal DMA IST
    //
    VOID*                   gDmacIntrEvent;

    //
    // Handle to DMA Interrupt Service Thread
    //
    VOID*                   gDmacIntrThread;

    //
    VOID*                   gDmacThreadReadyEvent;
    // Table of config information for all DMA channels
    //
// pkdebug. Not needed    DmaChannelConfigInfo    dmaChannelConfigInfo [XLLP_DMAC_CHANNEL_NUM];

    //
    // A channel array map for all DMA channels. Keeps track of channel allocation.
    // Could be moved to local config info table
    //
    XLLP_DMAC_CHANNEL_T     pArrayChannel[XLLP_DMAC_CHANNEL_NUM];

    //
    // Flag to keep track of first time initialization
    //
    BOOL                    notFirstTime;

    HANDLE                  hDMAEvent[32];

    XLLP_VUINT32_T          lastDCSR[32];

} DMA_GLOBALS;

//
// Event Object name. The existence of this object means that initialization
// of the General DMA Engine(GDE) has occured.
//
#define	GDE_FIRST_TIME_CHECK  (TEXT("GdeFirstTime"))
//
// Memory Mapped filename used by the GDE for interproces sharing of GDE data structures.
//
#define GDE_SHARED_FILENAME   (TEXT("GdeDmaGlobalsFile"))  // pkdebug. 



//
// Mask/Unmask DMAC Ints
//
#define INTC_DMAC_INT_EN(pINTC)        (pINTC |= XLLP_INTC_DMAC)
#define INTC_DMAC_INT_DIS(pINTC)       (pINTC &= ~XLLP_INTC_DMAC)

// Add -- Stop  - pkdebug . May need to move these items



//XLLP_STATUS_T	XllpDmacInit
//(
//);
P_XLLP_DMAC_T	GdeDmacInit
(
);

BOOL XllpDmacAcquireMutex
(    
    HANDLE  *hDmacMutex,
    DWORD   timeout
);

VOID XllpDmacFreeMutex
(    
    HANDLE  hDmacMutex
);

XLLP_UINT32_T   XllpDmacGetPhysicalAds  // pkdebug. Not needed.
(
 void*	virtualAddress
);


VOID* OSMemAlloc
(
    UINT32   numBytes
);

VOID OSMemFree
(
    VOID*           memPtr
);

XLLP_UINT32_T OSGetMaxPages
(
    XLLP_UINT8_T    *pBuffer,
    XLLP_UINT32_T   bufferLength
);

BOOL OSLockPhysicalPages
(
    VOID*           virtualAddress,
    XLLP_UINT32_T   numBytesToLock,
    XLLP_UINT32_T   *pageAddresses,
    XLLP_UINT32_T   osFlags
);

BOOL OSUnlockPhysicalPages
(
    VOID*           virtualAddress,
    XLLP_UINT32_T   numBytesToUnlock
);

void GdeDmacEnableInterrupt
(
  void
);

void GdeDmacDisableInterrupt
(
  void
);

XLLP_STATUS_T	GdeDmacAllocChannel(
	P_XLLP_DMAC_CHANNEL_T        pChannel,
	XLLP_DMAC_CHANNEL_PRIORITY_T aChannelPriority
);

void GdeDmacFreeChannel(
	XLLP_DMAC_CHANNEL_T       aChannel,
	XLLP_DMAC_DRCMR_T         aDeviceDrcmr
);


void GdeDmacFillLinkedDesc(
		P_XLLP_DMAC_DESCRIPTOR_T pDesc,
		P_XLLP_DMAC_DESCRIPTOR_T pNextDescPhyAddr,
		XLLP_DMAC_DESC_ENABLE_T  aStopContinue,
		XLLP_DMAC_DESC_BRANCH_T  aBranch,
		XLLP_UINT32_T            aSrcAddr,
		XLLP_UINT32_T            aTargetAddr,
		XLLP_DMAC_COMMAND_T*     pCmd
);





#endif